/*
 * Copyright (c) 2015 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShAccountDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShAccountService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShAccount;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * ShAccountService 实现类
 * @author KongXiangyu
 * @since 2015-09-07
 */
@Service("shAccountService")
public class ShAccountServiceImpl extends BaseServiceImpl<ShAccount, Long> implements ShAccountService {

    @Resource
    private ShAccountDao shAccountDao;

    public BaseDao<ShAccount, Long> getDao() {
        return shAccountDao;
    }

	@Override
	public DataGrid queryDataGrid(ShAccount account,PageHelper page) {
		DataGrid dataGrid = new DataGrid();
		ShAccount queryAccount = new ShAccount();
        if (!StringUtils.isBlank(page.getSort())) {
        	queryAccount.setOrderField(page.getSort());
        }
        if (!StringUtils.isBlank(page.getOrder())) {
        	queryAccount.setOrderFieldType(page.getOrder());
        }
        if (!StringUtils.isBlank(account.getName())) {//根据用户姓名
        	queryAccount.setName(account.getName());
        }
        if (null != account.getStatus()) {//用户状态
        	queryAccount.setStatus(account.getStatus());;
        }
        
        if (null != account.getGmtCreate()) {
        	queryAccount.addQueryData("gmtCreate", account.getGmtCreate());
        }
        if(null != account.getDoorId()){//根据门牌ID查询
        	queryAccount.setDoorId(account.getDoorId());
        }
        if(null != account.getUnitId()){//根据单元ID查询
        	queryAccount.setUnitId(account.getUnitId());
        }
        if(null != account.getBuildingId()){//根据楼栋ID查询
        	queryAccount.setBuildingId(account.getBuildingId());
        }
        if(null != account.getResidenceId()){//根据小区ID查询
        	queryAccount.setResidenceId(account.getResidenceId());
        }
        if(null != account.getTypeId()){//根据用户类型ID查询
        	queryAccount.setTypeId(account.getTypeId());
        }
        if(null != account.getLoginName()){//根据登录名查询
        	queryAccount.setLoginName(account.getLoginName());
        }
        if(null != account.getMobile()){//根据手机号查询
        	queryAccount.setMobile(account.getMobile());
        }
        if(null != account.getSex()){//根据性别查询
        	queryAccount.setSex(account.getSex());
        }
        
        long totalCount = shAccountDao.selectShAccountCountByCon(queryAccount);
        dataGrid.setTotal(totalCount);
        int startIndex = (page.getPage() - 1) * page.getRows();
        int endIndex = page.getPage() * page.getRows();
        if (totalCount > 0 && totalCount > startIndex) {
        	queryAccount.setStartIndex(startIndex);
        	queryAccount.setEndIndex(endIndex);
            List<ShAccount> deviceTypes = shAccountDao.selectShAccountByCon(queryAccount);
            dataGrid.setRows(deviceTypes);
        }
        return dataGrid;
	}

	@Override
	public DataGrid queryOnlineShAccounts(ShAccount queryShAccount, PageHelper page) {
		DataGrid dataGrid = new DataGrid();
        if (!StringUtils.isBlank(page.getSort())) {
        	queryShAccount.setOrderField(page.getSort());
        }
        if (!StringUtils.isBlank(page.getOrder())) {
        	queryShAccount.setOrderFieldType(page.getOrder());
        }
        
        long totalCount = shAccountDao.queryOnlineShAccountsCount(queryShAccount);
        dataGrid.setTotal(totalCount);
        int startIndex = (page.getPage() - 1) * page.getRows();
        int endIndex = page.getPage() * page.getRows();
        if (totalCount > 0 && totalCount > startIndex) {
        	queryShAccount.setStartIndex(startIndex);
        	queryShAccount.setEndIndex(endIndex);
            List<ShAccount> shAccounts = shAccountDao.queryOnlineShAccounts(queryShAccount);
            dataGrid.setRows(shAccounts);
        }
        return dataGrid;
	}

	@Override
	public DataGrid queryShAccounts(ShAccount queryShAccount, PageHelper page) {
		DataGrid dataGrid = new DataGrid();
        if (!StringUtils.isBlank(page.getSort())) {
        	queryShAccount.setOrderField(page.getSort());
        }
        if (!StringUtils.isBlank(page.getOrder())) {
        	queryShAccount.setOrderFieldType(page.getOrder());
        }
        
        long totalCount = shAccountDao.queryShAccountsCount(queryShAccount);
        dataGrid.setTotal(totalCount);
        int startIndex = (page.getPage() - 1) * page.getRows();
        int endIndex = page.getPage() * page.getRows();
        if (totalCount > 0 && totalCount > startIndex) {
        	queryShAccount.setStartIndex(startIndex);
        	queryShAccount.setEndIndex(endIndex);
            List<ShAccount> shAccounts = shAccountDao.queryShAccounts(queryShAccount);
            dataGrid.setRows(shAccounts);
        }
        return dataGrid;
	}

	@Override
	public List<String> getShAccountIds(ShAccount account) {
		return shAccountDao.getShAccountIds(account);
	}

	@Override
	public Long getOnlineNumber(ShAccount account) {
		return shAccountDao.queryOnlineShAccountsCount(account);
	}

}